//
//  CMDBAssociateViewController.swift
//  CMDB
//
//  Created by 王磊 on 09/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBAssociateViewController: NormalTitleViewController {
    
    private var associate: CMDBAssociateCollectionView = CMDBAssociateCollectionView.getInstance()
    
    override func setup() {
        super.setup()
        
        buildAssociate()
        
        loadData()
    }
}

extension CMDBAssociateViewController {
    
    private func loadData() {
        
        let works1 = CMDBWorksBean()
        
        works1.wid = "123"
        
        works1.poster = "http://7xwbs9.com1.z0.glb.clouddn.com/44a3778c8cd14ff0ba25f2a2baf223191479090176910"
        
        works1.name = "你是我的玫瑰 你是我的花"
        
        
        let works2 = CMDBWorksBean()
        
        works2.poster = "http://7xwbs9.com1.z0.glb.clouddn.com/44a3778c8cd14ff0ba25f2a2baf223191479090176910"
        
        works2.name = "你是我的玫瑰 你是我的花"
        
        works2.wid = "123"
        
        let works3 = CMDBWorksBean()
        
        works3.poster = "http://7xwbs9.com1.z0.glb.clouddn.com/44a3778c8cd14ff0ba25f2a2baf223191479090176910"
        
        works3.name = "你是我的玫瑰 你是我的花"
        
        works3.wid = "123"
        
        let works4 = CMDBWorksBean()
        
        works4.poster = "http://7xwbs9.com1.z0.glb.clouddn.com/44a3778c8cd14ff0ba25f2a2baf223191479090176910"
        
        works4.name = "你是我的玫瑰 你是我的花"
        
        works4.wid = "123"
        
        let works5 = CMDBWorksBean()
        
        works5.poster = "http://7xwbs9.com1.z0.glb.clouddn.com/44a3778c8cd14ff0ba25f2a2baf223191479090176910"
        
        works5.name = "你是我的玫瑰 你是我的花"
        
        works5.wid = "123"
        
        
        let works6 = CMDBWorksBean()
        
        works6.poster = "http://7xwbs9.com1.z0.glb.clouddn.com/44a3778c8cd14ff0ba25f2a2baf223191479090176910"
        
        works6.name = "你是我的玫瑰 你是我的花"
        
        works6.wid = "123"
        
        associate.updateData( [works1,works2,works3,works4,works5,works6])
        //        let asso = CMDBAssociateParam()
        //
        //        CMDBWorkManager.getInstance.queryAssociateMedia(asso, handler: { (associateList) in
        //
        //            if let associateList = associateList {
        //
        //                associate.updateData(associateList)
        //            }
        //
        //            printLog(associateList)
        //
        //            }, error: { (rescode) in
        //
        //
        //        }) {
        //
        //
        //        }
    }
    
}
extension CMDBAssociateViewController {
    
    private func buildAssociate() {
        
        contentView?.addSubview(associate)
        
        associate.frame = view.bounds
        
        associate.associateDelegate = self
        
    }
}
extension CMDBAssociateViewController: CMDBAssociateFooterViewDelegate {}

extension CMDBAssociateViewController {
    
    func associateWorks() {
        
        let associateToUser = CMDBAssociateToUserParam()
        
        associateToUser.associatedIds += associate.getSelectedWorksId()
        
        printLog(associateToUser.associatedIds)
        
        do {
            
            try CMDBWorkManager.getInstance.associateMediaToUser(associateToUser, handler: { (isSuccess) in
                
                UIApplication.sharedApplication().keyWindow?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
                
                }, error: { (rescode) in
                    
            }) {
                
            }
            
        } catch CMDBUserControllerExceptionType.ExceptionCode(code: CMDBUserControllerExceptionCodeType.UnSelectedAssociate) {
            
            CMDBShowInfoManager.getInstance.showInfoWithStatus("请选择关联作品")
            
        } catch {
            
            
        }
        
    }
}
